﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

using GameDataLibrary;

namespace TerricGame
{
    class ScriptScreen:GameScreen
    {
        ContentManager content;

        Texture2D bg;
        Texture2D scriptWindow;
        SpriteFont Yahei;

        Dictionary<string, Texture2D> backGrounds;
        Dictionary<string, Song> songs;

        Vector2 scriptWindowPostion;
        Vector2 scriptPostion;
        string script;
        Song song;

        public override void LoadContent()
        {
            if (content == null)
            {
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            }


            ScriptScreenSettings screenSettings = new ScriptScreenSettings();
            ScriptContent chapter = content.Load<ScriptContent>("Script/Script/ScriptContent01");
            backGrounds = chapter.BgPictures;
            scriptWindow = chapter.ScriptWindow;
            Yahei = chapter.FontType;
            //Yahei = content.Load<SpriteFont>("font/Yahei");

            scriptWindowPostion = new Vector2(43, 425);
            scriptPostion = new Vector2(43 + 10, 425 + 10);
            script = chapter.ScriptsName;


            songs = chapter.Songs;

            MediaPlayer.Volume = (float)0.5;
        }

        public override void UnloadContent()
        {
            content.Unload();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);


        }

        public override void HandleInput(InputState input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            KeyboardState keyboradState = input.CurrentKeyboardStates[(int)ControllingPlayer.Value];
            if (input.IsPauseGame(ControllingPlayer))
            {
                ScriptScreenQuit();
                ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
            }

            if (keyboradState.IsKeyDown(Keys.Space))
            {
                if (keyboradState.IsKeyUp(Keys.Space))
                {
 
                }
 
            }

            if (this.IsActive && MediaPlayer.State != MediaState.Playing)
            {
                MediaPlayer.Play(songs["OathSign"]);
            }
        }


        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            if (script == null)
                script = "";

            spriteBatch.Begin();
            spriteBatch.Draw(backGrounds["bg42_n"], new Rectangle(0, 0, 800, 600), Color.White);
            
            spriteBatch.Draw(scriptWindow, scriptWindowPostion, Color.White);
            spriteBatch.DrawString(Yahei, script, scriptPostion, Color.White);
            spriteBatch.End();
        }

        public override void ExitScreen()
        {
            base.ExitScreen();
            MediaPlayer.Stop();
        }
        

        private void ScriptScreenQuit()
        {
            if (MediaPlayer.State == MediaState.Playing)
            {
                MediaPlayer.Volume = (float)0.1;
            }
        }
    }
}
